package com.ruoyi.project.system.drivers.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.drivers.mapper.VeDriversMapper;
import com.ruoyi.project.system.drivers.domain.VeDrivers;
import com.ruoyi.project.system.drivers.service.IVeDriversService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@Service
public class VeDriversServiceImpl implements IVeDriversService 
{
    @Autowired
    private VeDriversMapper veDriversMapper;
@Autowired
private TaskService taskService;
@Autowired
private RuntimeService runtimeService;
    /**
     * 查询【请填写功能名称】
     * 
     * @param DriverID 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public VeDrivers selectVeDriversByDriverID(Long DriverID)
    {
        return veDriversMapper.selectVeDriversByDriverID(DriverID);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param veDrivers 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<VeDrivers> selectVeDriversList(VeDrivers veDrivers)
    {
        return veDriversMapper.selectVeDriversList(veDrivers);
    }

    @Override
    public List<VeDrivers> getVeDriversList() {
        return veDriversMapper.getVeDriversList();
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param veDrivers 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertVeDrivers(VeDrivers veDrivers)
    {
        return veDriversMapper.insertVeDrivers(veDrivers);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param veDrivers 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateVeDrivers(VeDrivers veDrivers)
    {
        return veDriversMapper.updateVeDrivers(veDrivers);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param DriverIDs 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteVeDriversByDriverIDs(String DriverIDs)
    {
        return veDriversMapper.deleteVeDriversByDriverIDs(Convert.toStrArray(DriverIDs));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param DriverID 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteVeDriversByDriverID(Long DriverID)
    {
        return veDriversMapper.deleteVeDriversByDriverID(DriverID);
    }


    @Transactional
    @Override
    public int submit(Long id, Long userId) {
        VeDrivers veDrivers = veDriversMapper.selectVeDriversByDriverID(id);


        if (veDrivers.getStatus()==0) {
            String processDefiKey = "vehicle";
            Map<String, Object> variables = new HashMap<String, Object>();
            variables.put("inputUser1", userId);

            ProcessInstance pi = runtimeService
                    .startProcessInstanceByKey(processDefiKey, variables);


            veDrivers.setFlowInstantId(Long.parseLong(pi.getProcessInstanceId()));
            veDrivers.setStatus(2);//状态(0-未提交，1-打回，2-审核中，3-已完成）
            return veDriversMapper.updateVeDrivers(veDrivers);
        }
        else {
            Long flowInstantId=veDrivers.getFlowInstantId();
            TaskQuery taskQuery = taskService.createTaskQuery();
            // 办理人的任务列表
            List<Task> list = taskQuery
                    .processInstanceId("" + flowInstantId)
                    .list();
            Task task=list.get(0);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("inputUser2", 3);
            taskService.complete(task.getId(),params);
            veDrivers.setStatus(2);//状态(0-未提交，1-打回，2-审核中，3-已完成）
            return  veDriversMapper.updateVeDrivers(veDrivers);
        }

    }
}
